package com.hua.yuan.live;

import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hua.yuan.base.BaseAppActivity;
import com.hua.yuan.live.adapter.PrivateMsgAdapter;
import com.hua.yuan.live.repertory.cloud_data.res.ChatMsgBean;
import com.hua.yuan.rtcroom.RTCRoom;
import com.hua.yuan.rtcroom.sdk.im.IMMessageMgr;

/**
 * @author richsjeson
 * 正在直播的页面
 */
public class PrivateChatActivity extends BaseAppActivity implements
        View.OnClickListener,
        IMMessageMgr.Callback,
        IMMessageMgr.IMMessageListener {

    private EditText edtContentInput;
    private TextView mTvSendMsg;
    private RecyclerView mRecycleView;
    private PrivateMsgAdapter mAdapter;
    private TextView tvTitle;
    private static Handler mHandler = new Handler();

    private String anchorRtcId;

    private RTCRoom mRtcRoom;


    @Override
    protected int initLayoutId() {
        return R.layout.activity_private_chat;
    }

    @Override
    protected void initView() {
        mRtcRoom=LiveModule.getRtcRoom(this);
        edtContentInput = (EditText) findViewById(R.id.edt_input_content_private_chat);
        mTvSendMsg = (TextView) findViewById(R.id.tv_submit_private_chat);
        mRecycleView = (RecyclerView) findViewById(R.id.msg_recycler_view);
        tvTitle = findViewById(R.id.titleView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mRecycleView.setLayoutManager(layoutManager);
        mAdapter = new PrivateMsgAdapter(this);
        mRecycleView.setAdapter(mAdapter);
        tvTitle.setText("私信");
        mTvSendMsg.setOnClickListener(this);
        mRtcRoom.getIMMessageMgr().setIMMessageListener(this);
        anchorRtcId = getIntent().getStringExtra(LiveModule.INTENT_ANCHOR_RTC_USER_ID);
    }

    /**
     * 初始化消息
     */
    private void initMessageData() {
        ChatMsgBean msg1 = new ChatMsgBean("你好.", ChatMsgBean.TYPE_RECEIVED);
        mAdapter.getData().add(msg1);
        ChatMsgBean msg2 = new ChatMsgBean("请问您有时间么", ChatMsgBean.TYPE_SENT);
        mAdapter.getData().add(msg2);
        ChatMsgBean msg3 = new ChatMsgBean("什么时候？", ChatMsgBean.TYPE_RECEIVED);
        mAdapter.getData().add(msg3);

    }

    @Override
    protected void initNet() {

    }

    @Override
    public void onClick(View v) {

        if (R.id.tv_submit_private_chat == v.getId()) {

            final String content = edtContentInput.getText().toString();
            if (!TextUtils.isEmpty(content)) {

                mRtcRoom.getIMMessageMgr().sendC2CCustomMessage(anchorRtcId, content, new IMMessageMgr.Callback() {
                    @Override
                    public void onError(int code, final String errInfo) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getApplicationContext(),"发送失败："+errInfo,Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                    @Override
                    public void onSuccess(Object... args) {
                        ChatMsgBean msg = new ChatMsgBean(content, ChatMsgBean.TYPE_SENT);
                        mAdapter.getData().add(msg);
                        mRecycleView.scrollToPosition(mAdapter.getData().size() - 1);
                        edtContentInput.setText("");
                    }
                });
            }
        }
    }

    @Override
    public void onError(int code, String errInfo) {

    }

    @Override
    public void onSuccess(Object... args) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(),"初始化成功：",Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onConnected() {

    }

    @Override
    public void onDisconnected() {

    }

    @Override
    public void onPusherChanged() {

    }

    @Override
    public void onGroupTextMessage(String groupID, String senderID, String userName, String headPic, final String message) {

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ChatMsgBean msg = new ChatMsgBean(message, ChatMsgBean.TYPE_RECEIVED);
                mAdapter.getData().add(msg);
                mRecycleView.scrollToPosition(mAdapter.getData().size() - 1);
            }
        });
    }

    @Override
    public void onGroupCustomMessage(String groupID, String senderID, String message) {

    }

    @Override
    public void onC2CCustomMessage(String sendID, String cmd, final String message) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ChatMsgBean msg = new ChatMsgBean(message, ChatMsgBean.TYPE_RECEIVED);
                mAdapter.getData().add(msg);
                mRecycleView.scrollToPosition(mAdapter.getData().size() - 1);
            }
        });
    }

    @Override
    public void onGroupDestroyed(String groupID) {

    }

    @Override
    public void onDebugLog(String log) {

    }
}

